let nums = [1, 3, 5, 6];
let target = 2;
var searchInsert = function (nums, target) {
    if (nums.length === 0) {
        return 0;
    }
    let left = 0;
    let right = nums.length - 1;
    while (left <= right) {
        // 取整
        let mid = Math.floor((left + right) / 2);
        if (nums[mid] === target) {
            return mid;
        } else if (nums[mid] > target) {
            // 缩小右边界
            right = mid - 1;
        } else {
            // 缩小左边界
            left = mid + 1;
        }
    }
    // 循环结束后，left 就是插入位置
    return left;
};

console.log(searchInsert(nums, target));